#include <iostream>
#include <list>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <cctype>
using namespace std;
typedef vector<short> vectorshort;

struct predicate {
    bool operator()( int x ) { return isdigit(x) & 0x01; }
};

short Hash(const vector<vectorshort>& in, map<short, vectorshort>& out) {
	vector<vectorshort>::const_iterator i = in.begin();
	map<short,vectorshort>::iterator it;
	short index = 0;
	vector<short> v;
	v.push_back(index);
	out.insert(pair<short, vectorshort>(i->front(), v));
	i++;
	index++;
	while(i != in.end()) {
		it = out.find(i->front());
		if(it != out.end()) {
			it->second.push_back(index);
		}
		else {
			vector<short> value;
			value.push_back(index);
			out.insert(pair<short, vectorshort>(i->front(), value));
		}
		i++;
		index++;
	}
}
void Majority(short size, const map<short, vectorshort>& out, vector<short>&winner, vector<short>& eliminate) {
	map<short,vectorshort>::const_iterator it;
	it = out.begin();
	short min = it->second.size();
	short maj = it->first;	
	short max = min;
	eliminate.push_back(maj);
	while(++it != out.end()) {
		if(min > it->second.size()) {
			eliminate.clear();
			eliminate.push_back(it->first);
			min = it->second.size();
		} else if(min == it->second.size()) {
			eliminate.push_back(it->first);
		}
		if(max < it->second.size()) {
			max = it->second.size();
			maj = it->first;
		}
	}
	double ratio = (double)((max * 1.0) / double(size/2))*100.0;
	if(ratio >= 50) {
		winner.push_back(maj);
	} else if(size == out.size() * out.find(eliminate.front())->second.size()) {
		winner = eliminate;
	}
	return;
}

void Majority(const vector<vectorshort>& in, map<short, vectorshort>& out, vector<short>&winner, vector<short>& eliminate) {
	Hash(in, out);
	// Get eliminations
	map<short,vectorshort>::iterator it;
	it = out.begin();
	short min = it->second.size();
	short maj = it->first;	
	short max = min;
	eliminate.push_back(maj);
	while(++it != out.end()) {
		if(min > it->second.size()) {
			eliminate.clear();
			eliminate.push_back(it->first);
			min = it->second.size();
		} else if(min == it->second.size()) {
			eliminate.push_back(it->first);
		}
		if(max < it->second.size()) {
			max = it->second.size();
			maj = it->first;
		}
	}
	double ratio = (double)((max * 1.0) / double(in.size()/2))*100.0;
	if(in.size() == out.size() * out[eliminate.front()].size()) {
		winner = eliminate;
	} else if(ratio >= 50) {
		winner.push_back(maj);
	}
	return;
}

void Contest(vector<vectorshort>& ballots, vector<short>& winner) {
	map<short, vectorshort> hash;
	vector<short> elimination;
	short nCandidate = ballots[0].size();
	Majority(ballots, hash, winner, elimination);
	while(nCandidate > 0) {
		if(winner.size() > 0)
			break;
		else {
			vector<short>::iterator it = elimination.begin();
			while(it != elimination.end()) {
				vector<short>::iterator indexes = hash[(*it)].begin();
				while(indexes != hash[(*it)].end()) {
					rotate(ballots[*indexes].begin(), ballots[*indexes].begin()+1, ballots[*indexes].end());
					// update hash
					map<short, vectorshort>::iterator p = hash.find(ballots[*indexes].front());
					if(p != hash.end()) {
						p->second.push_back(*indexes);
					} else {
						vector<short> value;
						value.push_back(*indexes);
						hash.insert(pair<short, vectorshort>(ballots[*indexes].front(), value));				
					}
					indexes++;
				}
				hash.erase(*it);
				it++;
			}
		}
		winner.clear();
		elimination.clear();
		Majority(ballots.size(), hash, winner, elimination);
		nCandidate--;
	}
	return;
}

int main() {
	unsigned short nCases = 0;
	string blank_line;
	cin >> nCases;
	cin >> noskipws;
	getline(cin, blank_line);
	while(nCases > 0) {
		cin >> skipws; 
		unsigned short nCandidate = 0;
		vector<string> names;
		vector<vectorshort> ballots;
		cin >> nCandidate;
		cin >> noskipws;
		string name;
		getline(cin, name);
		while(nCandidate-- > 0) {
			getline(cin, name);
			names.push_back(name.substr(0, name.size()-1));
		}
		string bstr;
		stringstream tokens(stringstream::in | stringstream::out);
		tokens >> skipws; 
		getline(cin, bstr);
		short bl = count_if(bstr.begin(), bstr.end(), predicate());
		while(bl > 0) {
			tokens << bstr;
			vector<short> choices;
			vector<short> uChoices;
			nCandidate = names.size();
			while(nCandidate-- > 0) {
				short choice = 0;
				tokens >> choice;
				if(choice < 1 || choice > names.size()) {
					goto NEXT;
				}
				choices.push_back(choice-1);
			}
			unique_copy(choices.begin(), choices.end(), back_inserter(uChoices));
			if(choices.size() == names.size() && choices.size() == uChoices.size())
				ballots.push_back(choices);
NEXT:			
			getline(cin, bstr);
			bl = count_if(bstr.begin(), bstr.end(), predicate());
		}
		vector<short> winner;
		if(ballots.size() > 0)
			Contest(ballots, winner);
		if(winner.size() > 0) {
			for(short i =0; i < winner.size(); i++)
			cout << names[winner[i]] << endl;
		 } else
			copy(names.begin(), names.end(), ostream_iterator<string>(cout, "\n"));
		nCases--;	
		if(nCases >= 1)
			cout << endl;
	}
	return 0;	
}
